package com.robot.console.config;

import com.robot.console.core.ServerInfo;
import org.jdom2.Element;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

public class CsConfig implements IConfig
{
	public static final String CLASS_NAME = "CsConfig";

	private String csServerHost;
	private int csServerPort;
	private int eventThreads;
	private int firstUserIdInRoom;
	private int ioThreads;
	private boolean neededReceiver;
	private boolean neededRenewer;
	private boolean needZip;
	private int needReconnectCount;
	private boolean needTalkInRoom;
	private String passWord;
	private int poolSize;
	private int roomIdStartNumber;
	private int roomSize;

	private long sendMessageInterval;

	private final ArrayList<ServerInfo> ServerList = new ArrayList<ServerInfo>();

	private String sitekey;

	private int userInRoomSize;

	private String version;

	@Override
	public void buildConfig(Element rootElement)
	{
		Element connectionElement = rootElement.getChild("connection");

		@SuppressWarnings("rawtypes")
		List serverList = connectionElement.getChildren();

		for (int i = 0; i < serverList.size(); i++)
		{
			Element serverElement = (Element) serverList.get(i);
			String host = serverElement.getAttribute("ip").getValue();
			if (host.equals("*"))
				try
				{
					InetAddress address = java.net.InetAddress.getLocalHost();
					host = address.getHostAddress();
				}
				catch (UnknownHostException ex)
				{
					host = "127.0.0.1";
				}
			ServerInfo sf = new ServerInfo(host, Integer.parseInt(serverElement.getAttribute("port").getValue()));
			this.getServerList().add(sf);
		}

		ServerInfo sf = this.ServerList.get(0);
		this.csServerHost = sf.getHost();
		this.csServerPort = sf.getPort();

		this.version = rootElement.getChild("version").getText();
		this.sitekey = rootElement.getChild("site-key").getText();
		this.poolSize = Integer.parseInt(rootElement.getChild("pool-size").getText());

		Element threadNumber = rootElement.getChild("thread-number");
		if (threadNumber != null)
		{
			int io_thread_num = Integer.parseInt(threadNumber.getChild("io-thread-num").getText());
			this.ioThreads = io_thread_num;

			int handler_thread_num = Integer.parseInt(threadNumber.getChild("handler-thread-num").getText());
			this.eventThreads = handler_thread_num;
		}

		Element neededRenewerElement = rootElement.getChild("need-renewer");
		if (neededRenewerElement != null)
		{
			this.neededRenewer = Boolean.parseBoolean(neededRenewerElement.getText());
		}

		Element neededReceiverElement = rootElement.getChild("need-receiver");
		if (neededReceiverElement != null)
		{
			this.neededReceiver = Boolean.parseBoolean(neededReceiverElement.getText());
		}

		Element neededZipElement = rootElement.getChild("need-zip");
		if (neededZipElement != null)
		{
			this.needZip = Boolean.parseBoolean(neededZipElement.getText());
		}

		Element sendMsgInterAttElement = rootElement.getChild("send-interval");
		if (sendMsgInterAttElement != null)
		{
			this.sendMessageInterval = Long.parseLong(sendMsgInterAttElement.getText());
		}

		Element firstUserIDInRoomElement = rootElement.getChild("first-user-id-in-room");
		if (firstUserIDInRoomElement != null)
		{
			this.firstUserIdInRoom = Integer.parseInt(firstUserIDInRoomElement.getText());
		}

		Element userInRoomSizElement = rootElement.getChild("user-in-room-size");
		if (userInRoomSizElement != null)
		{
			this.userInRoomSize = Integer.parseInt(userInRoomSizElement.getText());
		}

		Element roomIdStartNumberElement = rootElement.getChild("room-id-start-number");
		if (roomIdStartNumberElement != null)
		{
			this.setRoomIdStartNumber(Integer.parseInt(roomIdStartNumberElement.getText()));
		}

		Element roomSizeElement = rootElement.getChild("room-size");
		if (roomSizeElement != null)
		{
			this.roomSize = Integer.parseInt(roomSizeElement.getText());
		}

		Element needTalkInRoomElement = rootElement.getChild("need-room-talk");
		if (needTalkInRoomElement != null)
		{
			this.needTalkInRoom = Boolean.parseBoolean(needTalkInRoomElement.getText());
		}
		else
		{
			this.needTalkInRoom = false;
		}

		Element needReconnectCountElement = rootElement.getChild("need-reconnect-count");
		if (needReconnectCountElement != null)
		{
			this.needReconnectCount = Integer.parseInt(needReconnectCountElement.getText());
		}
		else
		{

			this.needReconnectCount = 100;
		}

	}

	public String getCsServerHost()
	{
		return csServerHost;
	}

	public int getCsServerPort()
	{
		return csServerPort;
	}

	public int getEventThreads()
	{
		return eventThreads;
	}

	public int getFirstUserIdInRoom()
	{
		return firstUserIdInRoom;
	}

	public int getIoThreads()
	{
		return ioThreads;
	}

	public int getNeedReconnectCount()
	{
		return needReconnectCount;
	}

	public String getPassWord()
	{
		return passWord;
	}

	public int getPoolSize()
	{
		return poolSize;
	}

	public int getRoomIdStartNumber()
	{
		return roomIdStartNumber;
	}

	public int getRoomSize()
	{
		return roomSize;
	}

	public long getSendMessageInterval()
	{
		return sendMessageInterval;
	}

	public ArrayList<ServerInfo> getServerList()
	{
		return ServerList;
	}

	@Override
	public String getSharedRedisHost()
	{
		return null;
	}

	@Override
	public int getSharedRedisPort()
	{
		return 0;
	}

	public String getSitekey()
	{
		return sitekey;
	}

	public int getUserInRoomSize()
	{
		return userInRoomSize;
	}

	public String getVersion()
	{
		return version;
	}

	public boolean isNeededReceiver()
	{
		return neededReceiver;
	}

	public boolean isNeededRenewer()
	{
		return neededRenewer;
	}

	public boolean isNeedZip()
	{
		return needZip;
	}

	public boolean isNeedTalkInRoom()
	{
		return needTalkInRoom;
	}

	public void setFirstUserIdInRoom(int firstUserIdInRoom)
	{
		this.firstUserIdInRoom = firstUserIdInRoom;
	}

	public void setNeedTalkInRoom(boolean needTalkInRoom)
	{
		this.needTalkInRoom = needTalkInRoom;
	}

	public void setRoomIdStartNumber(int roomIdStartNumber)
	{
		this.roomIdStartNumber = roomIdStartNumber;
	}

	public void setRoomSize(int roomSize)
	{
		this.roomSize = roomSize;
	}

	public void setUserInRoomSize(int userInRoomSize)
	{
		this.userInRoomSize = userInRoomSize;
	}

}
